package edu.pku.sei.ocl.value;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EModelElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.emf.ecore.util.EcoreEList;
import org.eclipse.emf.ecore.EStructuralFeature;

import edu.pku.sei.ocl.utilities.OCLUnmodifiableEcoreCollection;

@SuppressWarnings("unchecked")
public class OCLEcoreCollection implements
		OCLCollection{
	
	@Override
	public boolean isConstant() {
		if(content!=null){
			if(content.getEStructuralFeature()!=null&&
					content.getEStructuralFeature().isChangeable()==false)
				return true;
			else if(content.getEObject()!=null)
				return  content.getEObject() instanceof EModelElement;
		}
		return isConstant;
	}

	private boolean isConstant = false;
	@Override
	public void collectionLiteral(boolean flag) {
		isConstant = flag;
	}
	
	
	
	@Override
	public boolean equals(Object arg0) {
		if(arg0 instanceof OCLEcoreCollection)
			return content.equals(((OCLEcoreCollection) arg0).content);
		else if(arg0 instanceof OCLUnmodifiableEcoreCollection)
			return content.equals(((OCLUnmodifiableEcoreCollection) arg0).content);
		else
			return content.equals(arg0);
	}

	@SuppressWarnings("rawtypes")
	public EcoreEList content;

	@SuppressWarnings("rawtypes")
	public OCLEcoreCollection(EcoreEList result) {
		content = result;
	}

	@Override
	public void move(int newPosition, Object object) {
		content.move(newPosition, object);
	}

	@Override
	public Object move(int newPosition, int oldPosition) {
		return content.move(newPosition, oldPosition);
	}

	@Override
	public void add(int arg0, Object arg1) {
		content.add(arg0, arg1);
	}

	@Override
	public boolean add(Object arg0) {
		return content.add(arg0);
	}

	@Override
	public boolean addAll(Collection<? extends Object> arg0) {
		return content.addAll(arg0);
	}

	@Override
	public boolean addAll(int arg0, Collection<? extends Object> arg1) {
		return content.addAll(arg0, arg1);
	}

	@Override
	public void clear() {
		content.clear();
	}

	@Override
	public boolean contains(Object arg0) {
		return content.contains(arg0);
	}

	@Override
	public boolean containsAll(Collection<?> arg0) {
		return content.containsAll(arg0);
	}

	@Override
	public Object get(int arg0) {
		return content.get(arg0);
	}

	@Override
	public int indexOf(Object arg0) {
		return content.indexOf(arg0);
	}

	@Override
	public boolean isEmpty() {
		return content.isEmpty();
	}

	@Override
	public Iterator<Object> iterator() {
		return content.iterator();
	}

	@Override
	public int lastIndexOf(Object arg0) {
		return content.lastIndexOf(arg0);
	}

	@Override
	public ListIterator<Object> listIterator() {
		return content.listIterator();
	}

	@Override
	public ListIterator<Object> listIterator(int arg0) {
		return content.listIterator(arg0);
	}

	@Override
	public Object remove(int arg0) {
		return content.remove(arg0);
	}

	@Override
	public boolean remove(Object arg0) {
		return content.remove(arg0);
	}

	@Override
	public boolean removeAll(Collection<?> arg0) {
		return content.removeAll(arg0);
	}

	@Override
	public boolean retainAll(Collection<?> arg0) {
		return content.retainAll(arg0);
	}

	@Override
	public Object set(int arg0, Object arg1) {
		return content.set(arg0, arg1);
	}

	@Override
	public int size() {
		return content.size();
	}

	@Override
	public List<Object> subList(int arg0, int arg1) {
		return content.subList(arg0, arg1);
	}

	@Override
	public Object[] toArray() {
		return content.toArray();
	}

	@Override
	public Object[] toArray(Object[] arg0) {
		return content.toArray(arg0);
	}

	@Override
	public EClassifier getElementOclType() {
		return ((Setting) content).getEStructuralFeature().getEType();
	}

	@Override
	public CollectionTypeEnum getCollectionType() {
		boolean ord = ((Setting) content).getEStructuralFeature().isOrdered();
		boolean uni = ((Setting) content).getEStructuralFeature().isUnique();
		
		if(ord&&uni)
			return CollectionTypeEnum.OrderedSet;
		else if(ord&&!uni)
			return CollectionTypeEnum.Sequence;
		else if(!ord&&uni)
			return CollectionTypeEnum.Set;
		else return CollectionTypeEnum.Bag;
	}

	@Override
	public void removeAll(Object e) {
		while(remove(e));
	}
}
